<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>课程管理</title>
<link rel="stylesheet" href="/scriptZIP/layui.min.css">
	<link rel="stylesheet" href="../css/common2.css">
	<link rel="stylesheet" href="../css/urlty.css">
	<link rel="stylesheet" href="/scriptZIP/vue-select.min.css">
	<style>
		.state {
			color: white;
			border-radius: 3px;
			font-size: 14px;
			font-weight: bold;
			letter-spacing: 3px;
			padding: 5px 8px;
			background-color: #4BAF4F;
		}

		.conduct {
			background-color: #F8B551;
		}

		.end {
			background-color: #E64919;
		}

		.class-end {
			background-color: #9F9F9F;
		}

		html {
			position: relative;
		}

		#vue-root {
			padding-top: 20px;
		}

		.search_img {
			position: absolute;
			right: 3%;
		}

		.search {
			max-width: 120px;
			height: 33px;
			position: absolute;
			right: calc(2% + 60px + 147px);
		}

		.search-hours {
			right: calc(2% + 60px);
		}

		.box {
			border-radius: 15px;
			width: 40%;
			height: 60%;
			position: fixed;
			top: 25%;
			right: 25%;
			bottom: 25%;
			left: 25%;
			Z-index: 3;
			margin: auto;
			background-color: white;
			box-shadow: 0 0 8px #b1aeae;
			overflow: auto;
		}

		.v-select {
			display: inline-block;
			width: 150px;
		}

		.layui-btn {
			background-color: gray;
			height: unset;
			line-height: unset;
			padding: 5px 10px;
		}
	</style>

</head>

<body>
	<div id="vue-root">
		<form enctype="multipart/form-data" id="form" ref="form" style="display: none;">
			<input type="file" name="file" id="file" @change="importExcel" ref="file"
				accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel">
		</form>

		<a href="eliminate_course_item.html" style="color: white;margin-left: 2%;">
			<div class="add">添加课程</div>
		</a>
		<div class="refresh" onclick="location.reload()">刷新</div>

		<input placeholder="请输入课程名称" class="search" v-model="search.name" style="height: 33px;">
		<img src="../image/search.png" height="35" width="35" alt="" class="search_img" @click="getData0">
		<div class="table">
			<table class="table_top">
				<thead>
					<tr>
						<th class="one">
							<input type="checkbox" class="check-box" v-model="all">序号
						</th>
						<th class="two">课程名称</th>
						<th class="two">时间/课时</th>
						<th class="two">授课教师</th>
						<th class="two">课程费用</th>
						<th class="two">学习人数</th>
						<th class="two">课程状态</th>
						<th class="two">报名学生</th>
						<th>编辑/删除</th>
					</tr>
				</thead>
				<tbody id="list">
					<tr v-for="(lesson,index) in list" :key="lesson.id">
						<td class="one">
							<input type="checkbox" class="check-box" v-model="lesson.checked">
							<span v-text="index+1"></span>
						</td>
						<td class="two" v-text="lesson.name"></td>
						<td class="two">
							<template v-if="lesson.info.count">
								{{lesson.info.startDate}} 至 {{lesson.info.endDate}} <br>
								共{{lesson.info.count}}节课
							</template>
						</td>
						<td class="two" v-text="lesson.teacherName"></td>
						<td class="two" v-text="lesson.fee/100"></td>
						<td class="two" v-text="lesson.maxSign"></td>
						<td class="two" v-text="getStatus(lesson)"></td>
						<td class="two">
							<button @click="open(lesson.id)">报名学生</button>
							<!-- <a href="enroll_students.html" style="color: white"><button>报名学生</button></a> -->
						</td>
						<td>
							<div class="option">
								<div class="edit-icon option-icon" style="position: absolute; left: 20%; top: 30%;">
									<a :href="'eliminate_course_item.html?id='+lesson.id">
										<img class="img" src="../image/edit.png" height="20" width="20"
											onmouseover="this.src='../image/edit_light.png'"
											onmouseout="this.src='../image/edit.png'">
									</a>
								</div>

								<div class="delete-icon option-icon" style="right: 20%; top:30%;position: absolute;"
									@click="del([lesson.id])">
									<img class="img" src="../image/delete.png" height="20" width="20"
										onmouseover="this.src='../image/delete_light.png'"
										onmouseout="this.src='../image/delete.png'">
								</div>
							</div>
						</td>
					</tr>

				</tbody>
			</table>
		</div>
		<div class="delete" @click="batchDelete" v-if="total>1">批量删除</div>
		<ul class="pagination" v-if="pages>1">
			<li>
				<a v-if="pageNum!=1" @click="pageNum=1">«</a>
				<a class="pages-jump" v-if="pageNum!=1" @click="pageNum--">‹</a>
				<a v-text="pageNum"></a>
				<a class="pages-jump" v-if="pageNum!=pages" @click="pageNum++">›</a>
				<a v-if="pageNum!=pages" @click="pageNum=pages">»</a>
			</li>
		</ul>

		<!--遮罩层-->
		<div class="box" v-if="dialogShow">
			<div @click="dialogShow=false" align="right" style="margin-right: 10px;font-size: 20px">X</div>
			<h1 align="center">添加报名学生 </h1><br />
			<div style="text-align: center;margin-bottom: 10px;">
				<a href="/excel/lesson_student.xls" class="layui-btn">下载导入模板</a>
				<label for="file" type="button" class="layui-btn">批量导入</label>
			</div>

			<div align="center">
				<v-select placeholder="请选择班级" label="name" :options="classess" :reduce="item=>item.id"
					v-model="classesId"></v-select>
				<v-select placeholder="请选择学生" label="name" :options="students" :reduce="item=>item.id"
					v-model="studentId"></v-select>
				<button style="height:30px" @click="add">添加学生</button>
			</div>

			<table width="80%" border="0" style="margin-left: 50px" align="center">
				<tr align="center">
					<td>班级</td>
					<td>姓名</td>
					<td>总课时</td>
					<td>剩余课时</td>
					<td>操作</td>
				</tr>
				<hr />
				<tr align="center" v-for="buyer in buyers" :key="buyer.studentId">
					<td v-text="buyer.classesName"></td>
					<td v-text="buyer.studentName"></td>
					<td v-text="buyer.times||'不限'"></td>
					<td v-text="buyer.remain"></td>
					<td>
						<button @click="setTimes(buyer.studentId)">修改</button>
						<button v-if="!buyer.online" @click="delBuyer(buyer.studentId)">删除</button>
					</td>
				</tr>
			</table>

		</div>


	</div>

	<script src="/scriptZIP/qs.min.js"></script>
	<script src="/scriptZIP/vue.min.js"></script>
	<script src="/scriptZIP/axios.min.js"></script>

	<script src="/scriptZIP/dayjs.min.js"></script>
	<script src="/scriptZIP/vue-select.min.js"></script>

	<script src="/scriptZIP/dayjs.min.js"></script>
	<script src="/scriptZIP/vue-select.min.js"></script>


	<script src="/js/common.js"></script>
	<script src="/js/list.js"></script>

	<script>

		var schoolId = top_params().id;
		Vue.component('v-select', VueSelect.VueSelect);

		var app = new Vue(merge({
			data: {
				urls: {
					del: '/pc/lesson/del',
					list: '/pc/lesson/list/' + schoolId,
				},
				classess: [],
				students: [],
				classesId: null,
				dialogShow: false,
				buyers: [],
				lessonId: null,
				studentId: null,
			},
			created: function () {
				this.getData();
				axios.get('/pc/classes/all/' + schoolId).then(function (res) {
					this.classess = res.data;
				}.bind(this));

			},
			methods: {
				getStatus: function (lesson) {
					return dayjs().isBefore(dayjs(lesson.signEndTime)) ? '接受报名中' : '报名已截止';
				},
				getBuyers: function () {
					axios.get('/pc/lesson/student/' + this.lessonId).then(function (res) {
						this.buyers = res.data;
					}.bind(this));
				},
				open: function (id) {
					this.dialogShow = true;
					this.lessonId = id;
					this.getBuyers();
				},
				add: function () {
					if (!this.studentId) {
						alert('请选择学生');
						return;
					}
					axios.post('/pc/lesson/student/' + this.lessonId + '/' + this.studentId).then(function (res) {
						this.getBuyers();
					}.bind(this));
				},
				delBuyer: function (studentId) {
					if (!confirm('确认删除?')) return;
					axios.delete('/pc/lesson/student/' + this.lessonId + '/' + studentId).then(function (res) {
						this.getBuyers();
					}.bind(this));
				},
				setTimes: function (studentId) {
					var n = prompt('请输入总课时数量');
					if (!/\d+/.test(n)) return;
					axios.post('/pc/lesson/student/' + this.lessonId + '/' + studentId + '/times/' + n).then(function (res) {
						this.getBuyers();
					}.bind(this));
				},
				imported: function () {
					this.getBuyers();
					alert('成功');
					this.$refs.file.value = '';
				}
			},
			computed: {
				studentSet: function () {
					var o = {};
					this.buyers.forEach(function (it) {
						o[it.studentId] = true;
					});
					return o;
				}
			},
			watch: {
				classesId: function (newValue, oldValue) {
					this.studentId = null;
					if (newValue)
						axios.get('/pc/student/all/' + newValue).then(function (res) {
							this.students = res.data.filter(function (it) {
								return !(it.id in this.studentSet)
							}.bind(this));
						}.bind(this));
				},
				lessonId: function (newValue, oldValue) {
					this.urls.imports = '/pc/lesson/student/import/' + newValue;
				},
			}
		}));
	</script>

</body>

</html>